package com.csdj.article.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.article.common.CommonCons;
import com.csdj.article.common.WebResultMsgCode;
import com.csdj.article.entity.Article;
import com.csdj.article.entity.AttquesComment;
import com.csdj.article.entity.WebResult;
import com.csdj.article.service.IArticleService;
import com.csdj.article.service.IAttquesCommentService;
import com.csdj.article.service.RedisService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 评论表(问题和文章) 前端控制器
 * </p>
 *
 * @author Hh
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/attques/comment")
public class AttquesCommentController {
    @Resource
    private RedisService redisService;
    @Resource
    private IAttquesCommentService attquesCommentService;
    @Resource
    private IArticleService articleService;

    /**
     * 查看所有评论
     *
     * @return
     */
    @GetMapping()
    public WebResult getComment() {
        List<AttquesComment> list = attquesCommentService.list();
        return CollectionUtils.isEmpty(list) ? null : new WebResult(WebResultMsgCode.SUCCESS, list);
    }

    /**
     * 跟据文章id来查看文章的相关评论
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public WebResult getCommentAndId(@PathVariable("id") Integer id) {
        long count = articleService.count(Wrappers.<Article>lambdaQuery()
                .eq(Article::getArticleId, id)
                .last("limit 1"));
        if (count == 0) {
            return new WebResult(WebResultMsgCode.FAIL, CommonCons.ARTICLE_NOT_FUUND);
        }
        List<AttquesComment> attquesComment = attquesCommentService.list(Wrappers.<AttquesComment>lambdaQuery()
                .eq(AttquesComment::getCommentType, true)
                .eq(AttquesComment::getArticleId, id));
        WebResult webResult = new WebResult();

        if (CollectionUtils.isEmpty(attquesComment)) {
            webResult.setMsg(CommonCons.NO_COMMENT);
        } else {
            webResult.setCode(WebResultMsgCode.SUCCESS);
            webResult.putData("data", attquesComment);
        }

        return webResult;
    }

    /**
     * 添加评论
     *
     * @param attquesComment
     * @return
     */
    @PostMapping()
    public WebResult add(@RequestBody AttquesComment attquesComment) {

        WebResult webResult = new WebResult();
        if (Objects.isNull(attquesComment)
                || attquesComment.getArticleId() == null
                || attquesComment.getCommentContent() == null) {
            webResult.setMsg(CommonCons.PARAMET_ISNULL);
            return webResult;
        }
        Article article = articleService.getOne(Wrappers.<Article>lambdaQuery()
                .eq(Article::getArticleId, attquesComment.getArticleId())
                .select(Article::getId));
        if (Objects.isNull(article)) {
            webResult.setCode(WebResultMsgCode.FAIL);
            webResult.setMsg(CommonCons.ARTICLE_NOT_FUUND);
            return webResult;
        }
        //TODO 获取一下用户id 是谁评论的存进去
        //attquesComment.setCommentatorId();
        if (attquesCommentService.save(attquesComment)) {
            webResult.setCode(WebResultMsgCode.SUCCESS);
            //维护评论次数
            articleService.update(Wrappers.<Article>lambdaUpdate()
                    .setSql("answer_numb = answer_numb + 1")
                    .eq(Article::getId, article.getId()));
            return webResult;
        }
        webResult.setCode(WebResultMsgCode.USER_NO_LOGIN);
        return webResult;

    }

    /**
     * 评论分页
     *
     * @return
     */
    @GetMapping("page")
    public WebResult pageComment() {
        Page<AttquesComment> page = new Page<>(1, 5);
        IPage<AttquesComment> iPage = attquesCommentService.page(page);
        if (iPage != null) {
            return new WebResult(WebResultMsgCode.SUCCESS, iPage);
        } else {
            return new WebResult(WebResultMsgCode.USER_NO_LOGIN);
        }
    }

    /**
     * 用户点赞
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/giveLike/{name}")
    public WebResult giveLike(@PathVariable String name) {

        //redisService.sAdd("like:user", name);
        //TODO 每个用户分别都有自己对每个文章的点赞状态,key值肯定是唯一的,获取一下用户id
        redisService.sAdd(CommonCons.THUMBS + "用户id", name);
        //TODO 点完赞之后在哪里维护这个文章的点赞次数
        return new WebResult(WebResultMsgCode.SUCCESS, "点赞成功!");
    }

    /**
     * 用户取消点赞
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/giveCancelLike/{name}")
    public WebResult giveCancelLike(@PathVariable String name) {
        //redisService.srem("like:user", name);
        //TODO 获取一下用户id
        redisService.srem(CommonCons.THUMBS + "用户id", name);
        //TODO 取消完赞之后在哪里维护这个文章的点赞次数

        return new WebResult(WebResultMsgCode.SUCCESS, "取消点赞!");
    }

    /**
     * 用户是否点赞
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/isGiveLike/{name}")
    public WebResult isGiveLike(@PathVariable String name) {
        //TODO 获取一下用户id
        boolean member = redisService.isMember(CommonCons.THUMBS + "用户id", name);
        if (member) {
            return new WebResult(WebResultMsgCode.SUCCESS, "已点赞!");
        }
        return new WebResult(WebResultMsgCode.SUCCESS, "未点赞!");
    }

    /**
     * 获取用户点赞列表
     *
     * @return
     */
    @GetMapping(value = "/getGiveLikeList")
    public WebResult getGiveLikeList() {
        //TODO 这里获取到的只是点赞的那些文章,这样返给前端前端根本不知道是个什么东西，前端得到的将会是  1,2,3
        //TODO 存的时候如果用set去存，那么就要考虑要将set中的值取出来，一个一个对应好是哪些文章再返给前端
        //Set<Object> set = redisService.sMembers("like:user");

        Set<Object> set = redisService.sMembers(CommonCons.THUMBS + "用户id");
        //TODO 判空取出来之后遍历一下是哪些文章
        return new WebResult(WebResultMsgCode.SUCCESS, set);
    }

    /**
     * 获取点赞人数
     *
     * @return
     */
    @GetMapping(value = "/getGiveLikeSize")
    public WebResult getGiveLikeSize() {
        Long aLong = redisService.sCard("like:user");
        return new WebResult(WebResultMsgCode.SUCCESS, aLong);
    }
}
